//
//  ZZDetailCommentCell.m
//  Qzone
//
//  Created by zhangxy on 2018/6/20.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "ZZDetailCommentCell.h"

#import <SDWebImage/UIImageView+WebCache.h>

@implementation ZZDetailCommentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _imgAvatar.layer.cornerRadius = 22.0f;
    _imgAvatar.layer.masksToBounds = YES;
    
    [_labTitle setFont:FontFiftent];
    _labTitle.numberOfLines = 0;
    [_labTitle setTextColor:UIColorFromRGB(TextSizeOneColor)];
    
    
    [_labName setFont:FontFiftent];
    [_labName setTextColor:UIColorFromRGB(TextSizeOneColor)];
    
    [_btnLevel.titleLabel setFont:ListElevenFont];
    _btnLevel.layer.cornerRadius = 4.0f;
    _btnLevel.layer.masksToBounds = YES;
    _btnLevel.layer.borderWidth = 1.0f;
    _btnLevel.layer.borderColor = UIColorFromRGB(BorderLineColor).CGColor;
    [_btnLevel setTitleColor:UIColorFromRGB(TextSizeOneColor) forState:0];
    [_btnLevel setImage:[UIImage imageNamed:@"btn_collect"] forState:0];
    
    
    [_labTime setFont:FontThirteen];
    [_labTime setTextColor:UIColorFromRGB(BgLineColor)];
    
    [self setBtnStype:_btnVip image:@"Combined_Shape"];
}

-(void)setBtnStype:(UIButton *) btn image:(NSString *)imgName{
    [btn setImage:[UIImage imageNamed:imgName] forState:0];
    [btn setTitleColor:UIColorFromRGB(TextSizeOneColor) forState:0];
    [btn.titleLabel setFont:ListElevenFont];
    [btn setTitle:@"" forState:0];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 2, 0, 0)];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btn setBackgroundColor:UIColor.clearColor];
}


-(void) moreComment:(UITapGestureRecognizer *) tap{
    
}


-(void)dataToView:(ZZTopicCommentEntity *) topic{
    self.backgroundColor = [UIColor whiteColor];
    [self setUserViewData:topic];
    
    self.frame = CGRectMake(0, 0, ScreenWidth, CGRectGetMaxY(_commentView.frame)+20);
}

-(void)setUserViewData:(ZZTopicCommentEntity *) topic{
    [_imgAvatar sd_setImageWithURL:[NSURL URLWithString:convertToString(topic.user_avatar)] placeholderImage:[UIImage imageNamed:@"default_image"]];
    [_labName setText:convertToString(topic.user_name)];
    CGRect lf = _btnLevel.frame;
    CGSize s = [self autoWidthOfLabel:_labName with:21];
    lf.origin.x = s.width + CGRectGetMaxX(_labName.frame) + 5;
    _btnLevel.frame = lf;
    [_btnLevel setTitle:[NSString stringWithFormat:@"  Lv %@",@""] forState:0];
    [_btnLevel setContentEdgeInsets:UIEdgeInsetsMake(2, 0, 2, 0)];
    [_btnVip setFrame:CGRectMake(CGRectGetMaxX(lf) + 5, lf.origin.y, 52, 18)];
    [_labTime setText:topic.add_time];
    
    [_labTitle setText:topic.message];
    
    [self autoWidthOfLabelHegith:_labTitle with:ScreenWidth - 40];
    
    [self addCommentView:topic.thread_list fy:CGRectGetMaxY(_labTitle.frame)+(topic.thread_list.count == 0 ? 0 : 10)];
}

-(UIImageView *)createImageView{
    UIImageView *imgView = [[UIImageView alloc] init];
    [imgView setContentMode:UIViewContentModeScaleAspectFill];
    imgView.clipsToBounds = YES;
    return imgView;
}

-(UILabel *) createLabel{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setTextAlignment:NSTextAlignmentLeft];
    [label setFont:FontThirteen];
    [label setTextColor:UIColorFromRGB(TextSizeOneColor)];
    [label setBackgroundColor:UIColor.clearColor];
    [label setNumberOfLines:0];
    return label;
}

-(void)addCommentView:(NSMutableArray *) array fy:(CGFloat) y{
    [_commentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat iy = 10;
    if(array.count == 0){
        iy = 0;
    }
    CGFloat maxWidth = ScreenWidth - 74;
    for (ZZTopicCommentEntity *entity in array) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, iy, maxWidth, 0)];
        [label setTextAlignment:NSTextAlignmentLeft];
        [label setFont:FontThirteen];
        [label setTextColor:UIColorFromRGB(TextSizeOneColor)];
        [label setBackgroundColor:UIColor.clearColor];
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@:%@",entity.user_name,entity.message]];
        CGFloat len = [NSString stringWithFormat:@"%@:",entity.user_name].length;
        [string addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(BgTitleColor) range:NSMakeRange(0, len)];
        
        [string addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(TextSizeOneColor) range:NSMakeRange(len, string.length - len)];
        label.attributedText = string;
        
        CGSize s = [self autoWidthOfLabelHegith:label with:label.frame.size.width];
        
        iy = s.height + iy + 7;
        [_commentView addSubview:label];
        
    }
    
    if(array.count > 2){
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, iy, maxWidth, 21)];
        [label setTextAlignment:NSTextAlignmentLeft];
        [label setFont:FontThirteen];
        [label setTextColor:UIColorFromRGB(Text6Color)];
        [label setBackgroundColor:UIColor.clearColor];
        [label setText:@"查看全部评论..."];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moreComment:)];
        [label addGestureRecognizer:tap];
        [_commentView addSubview:label];
        iy = iy + 21 + 10;
    }
    
    [_commentView setFrame:CGRectMake(10, y, ScreenWidth - 40, iy)];
    
}



/**
 计算Label高度
 
 @param label 要计算的label，设置了值
 @param width label的最大宽度
 */
- (CGSize )autoWidthOfLabel:(UILabel *)label with:(CGFloat )height{
    //Calculate the expected size based on the font and linebreak mode of your label
    // FLT_MAX here simply means no constraint in height
    CGSize maximumLabelSize = CGSizeMake(FLT_MAX, height);
    
    CGSize expectedLabelSize = [label sizeThatFits:maximumLabelSize];
    
    //adjust the label the the new height.
    CGRect newFrame = label.frame;
    newFrame.size.width = expectedLabelSize.width;
    label.frame = newFrame;
    [label updateConstraintsIfNeeded];
    
    return expectedLabelSize;
}

- (CGSize )autoWidthOfLabelHegith:(UILabel *)label with:(CGFloat )width{
    //Calculate the expected size based on the font and linebreak mode of your label
    // FLT_MAX here simply means no constraint in height
    CGSize maximumLabelSize = CGSizeMake(width, FLT_MAX);
    
    CGSize expectedLabelSize = [label sizeThatFits:maximumLabelSize];
    
    //adjust the label the the new height.
    CGRect newFrame = label.frame;
    newFrame.size.height = expectedLabelSize.height;
    label.frame = newFrame;
    [label updateConstraintsIfNeeded];
    
    return expectedLabelSize;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
